package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author xueyoupeng
* @description 针对表【base_attr_info(属性表)】的数据库操作Service实现
* @createDate 2023-07-11 00:23:45
*/
@SuppressWarnings("ALL")
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo> implements BaseAttrInfoService{

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueService baseAttrValueService;


    @Override
    public List<BaseAttrInfo> findBaseAttrInfo(Long c1Id, Long c2Id, Long c3Id) {
        return baseAttrInfoMapper.findBaseAttrInfo(c1Id,c2Id,c3Id);
    }

    @Override
    @Transactional //涉及到多表操作 要开启事务
    public void saveBaseAttrInfo(BaseAttrInfo baseAttrInfo) {

        //对应的是保存和修改的实现 看参数是否有id存在
        Long attrInfoId = baseAttrInfo.getId();

        if (attrInfoId == null){
            //保存平台属性
            this.save(baseAttrInfo);

            //保存平台属性值
            //没有返回attr_id字段
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            saveBaseAttrValues(attrValueList , attrInfoId);

//            List<BaseAttrValue> baseAttrValues = attrValueList.stream().map(baseAttrValue -> {
//                baseAttrValue.setAttrId(attrInfoId);
//                return baseAttrValue;
//            }).collect(Collectors.toList());
//            baseAttrValueService.saveBatch(baseAttrValues);
        }else {
            //修改平台属性名称
            this.updateById(baseAttrInfo);

            //更新操作 要先删除原有的数据 再重新添加
            //根据id删除
            deleteBaseAttrValuesByAttrId(attrInfoId);

            //保存平台属性值
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            saveBaseAttrValues(attrValueList , attrInfoId);


//            List<BaseAttrValue> baseAttrValues = attrValueList.stream().map(baseAttrValue -> {
//                baseAttrValue.setId(null);
//                baseAttrValue.setAttrId(attrInfoId);
//                return baseAttrValue;
//            }).collect(Collectors.toList());
//            //修改平台属性值
//            baseAttrValueService.saveBatch(baseAttrValues);
        }
    }

    /**
     * 保存平台属性值
     */
    private void saveBaseAttrValues(List<BaseAttrValue> attrValueList, Long attrInfoId) {
        List<BaseAttrValue> baseAttrValues = attrValueList.stream().map(baseAttrValue -> {
            baseAttrValue.setAttrId(attrInfoId);
            baseAttrValue.setId(null); // 保证插入时没有ID
            return baseAttrValue;
        }).collect(Collectors.toList());

        baseAttrValueService.saveBatch(baseAttrValues);
    }

    /**
     * 根据attrId删除平台属性值
     */
    private void deleteBaseAttrValuesByAttrId(Long attrInfoId) {
        LambdaQueryWrapper<BaseAttrValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseAttrValue::getAttrId, attrInfoId);
        baseAttrValueService.remove(lambdaQueryWrapper);
    }
}




